package com.example.enjoyarkuix.bridge;

import static android.widget.Toast.LENGTH_SHORT;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

import ohos.ace.adapter.ALog;
import ohos.ace.adapter.capability.bridge.BridgeManager;
import ohos.ace.adapter.capability.bridge.BridgePlugin;
import ohos.ace.adapter.capability.bridge.IMessageListener;
import ohos.ace.adapter.capability.bridge.IMethodResult;
import ohos.ace.adapter.capability.bridge.MethodData;

public class BridgeTest extends BridgePlugin implements IMessageListener, IMethodResult {

    public static int STRESS_TEST = 0;
    public static String STRESS_TEST_MESSAGE = "";

    private final Handler mainHandler = new Handler(Looper.getMainLooper());

    public String name;
    private Context context;

    public BridgeTest(Context context, String name, BridgeManager bridgeManager) {
        super(context, name, bridgeManager);
        this.name = name;
        this.context = context;
        setMethodResultListener(this);
        setMessageListener(this);
    }

    public BridgeTest(Context context, String name, BridgeManager bridgeManager, BridgeType codecType) {
        super(context, name, bridgeManager, codecType);
        this.name = name;
        this.context = context;
        setMethodResultListener(this);
        setMessageListener(this);
    }

    public String getName() {
        return this.name;
    }


    public String testBridge_NativeToTs_callMethod() {
        ALog.e("BridgeTest", "TS->原生侧方法 call android testBridge_NativeToTs_callMethod call success, param: ");
        String[] data = {"message", "from", "android"};
        sendMessage(data);
        return "call success";
    }


    public String testBridge_TsToNative_callMethod(Object s1) {
        String s = s1.toString();
        ALog.e("BridgeTest testCallBack", "TS->原生侧方法 call android testVoidFun call success, param: " + s);
        return BridgeUtil.getParamData(s);
    }

    public String testCallBack(Object s1) {
        ALog.e("BridgeTest ", "testCallBack TS->原生侧方法 call android testCallBack call success, param: ");
        Object[] androidParam = {"hello my android"};
        MethodData methodData = new MethodData("testCallBack", androidParam);
        callMethod(methodData);
        return "testCallBack call success";
    }


    public void testCallBackParams(String sParam, int iParam, boolean bParam) {
        ALog.i("BridgeTest testCallBackString send param is ", sParam);
        Object[] androidParam = {sParam, iParam, bParam};
        MethodData methodData = new MethodData("testCallBackParams", androidParam);
        callMethod(methodData);
    }

    public String testCallBackReturn(String sParam) {
        ALog.i("BridgeTest testCallBackReturn send param is ", sParam);
        Object[] androidParam = {"android hello world"};
        MethodData methodData = new MethodData("testCallBackReturn", androidParam);
        callMethod(methodData);
        return "The parameter of js is " + sParam;
    }

    @Override
    public Object onMessage(Object object) {
        return "android receive success";
    }

    @Override
    public void onMessageResponse(Object jsonObject) {
        ALog.i("onMessageResponse data:", jsonObject.toString());
        STRESS_TEST_MESSAGE = jsonObject.toString();
        if (STRESS_TEST == 0) {
            mainHandler.post(() -> {
                Toast.makeText(this.context, jsonObject.toString() + "  ", LENGTH_SHORT).show();
            });
        }
    }

    @Override
    public void onSuccess(Object o) {
        if (o == null) {
            ALog.i("bridge return data is null", " call Success");
            mainHandler.post(() -> {
                Toast.makeText(this.context, "callMethod success, The method is of type void", LENGTH_SHORT).show();
            });
        } else {
            ALog.i("bridge onSuccess data:", o.toString());
            mainHandler.post(() -> {
                Toast.makeText(this.context, o.toString() + "  ", LENGTH_SHORT).show();
            });
        }
    }

    @Override
    public void onError(String s, int i, String s1) {
        ALog.e("BridgeTest:", "onError: " + s + " " + String.valueOf(i) + s1);
        mainHandler.post(() -> {
            Toast.makeText(this.context, "onError data:" + s + String.valueOf(i) + s1 + "  ", LENGTH_SHORT).show();
        });
    }

    @Override
    public void onMethodCancel(String s) {
        ALog.i("BridgeTest:", "onMethodCancel");
        mainHandler.post(() -> {
            Toast.makeText(this.context, "onError data:" + s + "  ", LENGTH_SHORT).show();
        });
    }
}